<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class Login extends CI_Controller {

	/**
	 * 用户登录
	 */
	public function index(){	
		$this->load->library('Cismarty');
		$this->load->library('session');
		//$this->load->library('encrypt');
		$this->load->model('Authority');
		$username=$_POST['username'];
		$password=$_POST['password'];
		
		if(!empty($username)&&!empty($password)){
			$result=$this->Authority->getLoginUser($username,$password);
		}
		if(!$result||count($result)==0){
			$username=$this->session->userdata('username');
			$password=$this->session->userdata('password');
			
			$this->cismarty->assign('username',$username);
			$this->cismarty->assign('password',$password); 
			$this->cismarty->assign('isLogin',false);
			
			$this->cismarty->assign('baseUrl',$this->config->item('base_url')); 
			$this->cismarty->assign('loginMsg','用户名或密码错误，请重新登录！'); 
			$this->cismarty->display('login.tpl'); 
			exit;
		}else{
			$username=$result[0]->username;
			$userData = array(
				'username'  => $username,
				'password'     => $result[0]->password,
				'isLogin' => true
			);
			$this->session->set_userdata($userData);
			$menuList=$this->Authority->getMenuByUId($result[0]->id);
			$this->cismarty->assign('menuList',$menuList); 
			$this->cismarty->assign('username',$username); 
			$this->cismarty->assign('isLogin',true);
			$this->cismarty->display('main.tpl'); 
			//echo $result[0]->birthday;
		}
		
		//$this->cismarty->assign('nickname',$result->nickname);  
		//$this->cismarty->assign('password',$password);  
		//$this->cismarty->assign('password',$password);  
	}
	
	
	public function logout(){
		$this->load->library('session');
		$this->load->library('Cismarty');
		
		$username=$this->session->userdata('username');
		$password=$this->session->userdata('password');
		
		$this->session->set_userdata('isLogin', false);
		$this->cismarty->assign('isLogin',false);
			
		$this->cismarty->assign('username',$username);
		$this->cismarty->assign('password',$password); 	
		
		$this->cismarty->assign('baseUrl',$this->config->item('base_url')); 
		$this->cismarty->assign('loginMsg','己安全退出登录！'); 
		$this->cismarty->display('login.tpl'); 	
	}
	/**
	 * 通过用户ID去读取对应的权限
	 * 实使化用户菜单
	 */
//	public function getMenuById(userId)
//	{	
//		
//	}
}

/* End of file welcome.php */
/* Location: ./application/controllers/welcome.php */